import http from "../../utils/http";
import {Api} from "../../utils/api";

Page({
  data: {
    companyInfo: {
      name: '进销存测试公司',
      productNum: 0,
      materialNum: 0,
      todayPutNum: 0,
    },
    optionGroupList: [
      {
        title: '常用模块',
        optionList: [
          {icon: 'renyuanguanli', name: '今日完成量', option: 'customer'},
          {icon: 'renyuanguanli', name: '本月完成量', option: 'setting'},
          {icon: 'renyuanguanli', name: '今日耗材', option: 'setting'},
          {icon: 'renyuanguanli', name: '本月耗材', option: 'setting'},
          {icon: 'renyuanguanli', name: '物料采购', option: 'customer'},
          {icon: 'renyuanguanli', name: '产品出库', option: 'setting'}
        ]
      },
      {
        title: '管理模块',
        optionList: [
          {icon: 'renyuanguanli', name: '员工管理', option: 'employee'},
          {icon: 'renyuanguanli', name: '客户管理', option: 'customer'},
          {icon: 'renyuanguanli', name: '供应商管理', option: 'supplier'},
          {icon: 'renyuanguanli', name: '产品管理', option: 'setting'},
          {icon: 'renyuanguanli', name: '物料管理', option: 'customer'},
        ]
      }
    ],
    userCompanyList: [],
    showCompanyPopupVisible: false,
  },
  onShow() {
    this.getTabBar().setData({active: 'index'})
    this.getCompanyInfo()
    this.getCompanyList()
    this.onCloseCompanyPopup()
  },
  toOption(e) {
    const option = e.currentTarget.dataset.option
    switch (option) {
      case 'customer': wx.navigateTo({url: '/pages/customer/index'}); break;
      case 'supplier': wx.navigateTo({url: '/pages/supplier/index'}); break;
      case 'employee': wx.navigateTo({url: '/pages/employee/index'}); break;
    }
  },
  onShowCompanyPopup() {
    this.setData({showCompanyPopupVisible: true})
  },
  onCloseCompanyPopup() {
    this.setData({showCompanyPopupVisible: false})
  },
  selectCompany(e) {
    const id = e.currentTarget.dataset.id
    const _this = this
    wx.showLoading({title: '切换中...'})
    http.postRequest(Api.changeCompany, {id}, (res) => {
      wx.hideLoading()
      _this.getCompanyInfo()
      _this.getCompanyList()
      _this.onCloseCompanyPopup()
    })
  },
  getCompanyInfo() {
    http.getRequest(Api.getCompanyInfo, {}, (res) => {
      this.setData({companyInfo: res.data})
    })
  },
  getCompanyList() {
    http.getRequest(Api.getCompanyList, {}, (res) => {
      this.setData({userCompanyList: res.data})
    })
  },
  toCompanyForm() {
    wx.navigateTo({
      url: '/pages/company/form/index',
    })
  }
})
